package slowka;

import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.Vector;

/**
 * Contains test's questions and status
 * @author ssobczak
 */
public class QuestionsSuite {
    /**
     * Creates QuestionsSuite object
     * @param suites list of suites to load questions from
     * @param mode mode of questions asking
     * @param rb translations
     */
    public QuestionsSuite(List<Suite> suites, QuestionAskingMode mode, ResourceBundle rb) {
        toAsk = new Vector<Question>();
        done = new Vector<Question>();

        prepareQuestions(suites, mode, rb);
        questionsTotal = questionsRemaning = toAsk.size();

        drawNextQuestion();
    }

    /**
     * Creates questions from suites
     * @param suites List of suites to load questions from
     * @param mode mode of questions asking
     * @param rb translations
     */
    private void prepareQuestions(List<Suite> suites, QuestionAskingMode mode, ResourceBundle rb) {
        for(Suite s : suites)
            for(Translation t : s.getTranslations()) {
                t.setFromToOrder(rb.getString(mode.name()));
                toAsk.add(new Question(s.getName(), t));
            }
    }

    /**
     * Checks answer and updates internal statistics
     * @param ans answer to be checked
     * @return true if ans is a correct answer for current question
     */
    public boolean checkAnswer(String ans) {
        if(currentQuestion().checkAnswer(ans)) {
            countGoodAnswer();

            if(currentQuestion().canDelete()) {
                done.add(currentQuestion());
                toAsk.remove(currentQuestionNumber);
            }

            return true;
        }
        else {
            countWrongAnswer();
            return false;
        }
    }

    public String getCurrentAnswer() {
        return currentQuestion().getAnswer();
    }

    public int getTotalCount() {
        return questionsTotal;
    }

    public int getRemaningCount() {
        return questionsRemaning;
    }

    /**
     * Draws next question
     * @return true if there is a question to be asked
     */
    public boolean drawNextQuestion() {
        Random rand = new Random();

        if(toAsk.isEmpty())
            return false;

        currentQuestionNumber = rand.nextInt(toAsk.size());
        return true;
    }

    public Question currentQuestion() {
        return toAsk.get(currentQuestionNumber);
    }

    /**
     * Returns number of perfectly answered questions
     * @return number of perfectly answered questions
     */
    public int getPerfectCount() {
        int ret = 0;

        for(Question q : done)
            if(q.isPerfect())
                ret++;
        
        return ret;
    }

    /**
     * Get questions, that were answered wrong
     * @return vector of questions that were answered wrong
     */
     public Vector<Question> getWrongAnswers() {
        Vector<Question> ret = new Vector<Question>();

        for(Question q : done)
            if(!q.isPerfect())
                ret.add(q);

        return ret;
    }

    /** updates internal stats */
    private void countGoodAnswer() {
        questionsRemaning--;
    }

    /** updates internal stats */
    private void countWrongAnswer() {
        questionsRemaning++;
        questionsTotal++;
    }

    private Vector<Question> toAsk, done;
    private int currentQuestionNumber;
    private int questionsTotal, questionsRemaning;
}
